/*
 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "los_task.h"
#include "ohos_init.h"
#include "picture.h"
#include "lcd.h"
EVENT_CB_S lcd_event;
extern EVENT_CB_S event_wild;
extern EVENT_CB_S event_water;

uint8_t iowild_status = 0;
uint8_t iowater_status = 0;
uint8_t iosoild_status = 0;

extern unsigned int lcd_msg;

#define MSG_QUEUE_LENGTH                                16
#define BUFFER_LEN                                      50

typedef struct
{
    float luminance;/*亮度*/
    float humidity;/*湿度*/
    float temperature;/*温度*/
} e53_ia_data_t;

extern const unsigned char gImage_lingzhi[IMAGE_MAXSIZE_LINGZHI];

/**开机界面 */
void lcd_start(void *arg)
{
    uint32_t lcd_ret = 0;
    lcd_ret = lcd_init();
    if (lcd_ret != 0)
    {
        printf("lcd_init failed(%d)\n", lcd_ret);
        return;
    }
    //初始化
    
    unsigned int ret = LOS_OK;
    float t = 0;
    uint8_t titel[] = "智慧农业";
    uint8_t subtitela[] = "出品者：张岚";
    uint8_t subtitelb[] = "陆云杰";
    lcd_fill(0, 0, LCD_W, LCD_H, LCD_WHITE);
    printf("初始界面\n");
    lcd_show_picture(15, 0, 210, 62, &gImage_lingzhi[0]);
    lcd_show_chinese(56, 70, titel, LCD_RED, LCD_WHITE, 32, 0);
    lcd_show_chinese(70, 200, subtitela, LCD_RED, LCD_WHITE, 24, 0);
    lcd_show_chinese(166, 224, subtitelb, LCD_RED, LCD_WHITE, 24, 0);
    //展示初始界面
    LOS_Msleep(5000);
    lcd_fill(15, 0, 210, 62, LCD_WHITE);
    lcd_show_chinese(56, 70, titel, LCD_WHITE, LCD_WHITE, 32, 0);
    lcd_show_chinese(70, 200, subtitela, LCD_WHITE, LCD_WHITE, 24, 0);
    lcd_show_chinese(166, 224, subtitelb, LCD_WHITE, LCD_WHITE, 24, 0);
    printf("休眠2s\n");
    LOS_Msleep(2000);
    printf("开始写事件_lcd\n");
    ret = LOS_EventWrite(&lcd_event, 0x00000001);

}

/**数据界面 */
void lcd_process(void *arg)
{
    unsigned int event;
    event = LOS_EventRead(&lcd_event, 0x00000001, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
    printf("开始读事件_lcd\n");

    unsigned int ret = LOS_OK;
    e53_ia_data_t addr={24.00,24.00,24.00};
    uint32_t lcd_ret = 0;
    uint8_t data_title[] = "数据显示";
    uint8_t data_l[] = "明度：";
    uint8_t data_h[] = "湿度：";
    uint8_t data_t[] = "温度：";
    uint8_t data_wild[] = "通风扇：";
    uint8_t data_water[]= "抽水泵：";
    uint8_t data_io1[]= "开";
    uint8_t data_io0[]= "关";

    //lcd_fill(0, 0, LCD_W, LCD_H, LCD_WHITE);
    lcd_show_chinese(56,70, data_title, LCD_RED, LCD_WHITE, 32, 0);
    lcd_show_chinese(20, 120, data_l, LCD_RED, LCD_WHITE, 24, 0);
    lcd_show_chinese(20, 150, data_h, LCD_RED, LCD_WHITE, 24, 0);
    lcd_show_chinese(20, 180, data_t, LCD_RED, LCD_WHITE, 24, 0);
    lcd_show_chinese(20, 240, data_wild, LCD_RED, LCD_WHITE, 24, 0);
    lcd_show_chinese(20, 270, data_water, LCD_RED, LCD_WHITE, 24, 0);
    LOS_Msleep(2000);
    printf("开始写事件_lcd\n");
    ret = LOS_EventWrite(&lcd_event, 0x10000001);
    
    while (1)
    {
        ret = LOS_QueueReadCopy(lcd_msg, (void *)&addr, BUFFER_LEN, LOS_WAIT_FOREVER);
        if (ret == LOS_OK)
        {   
            printf("读取 亮度:%f 湿度:%f 温度：%f\n",addr.luminance,addr.humidity,addr.temperature);
        }
        else
        {
            printf("%s read Message Queue fail ret:0x%x\n", __func__, ret);
        }
        printf("开始打印数据\n");
        lcd_show_float_num1(100,120,addr.luminance,4,LCD_RED,LCD_WHITE,24);
        lcd_show_float_num1(100,150,addr.humidity,4,LCD_RED,LCD_WHITE,24);
        lcd_show_float_num1(100,180,addr.temperature,4,LCD_RED,LCD_WHITE,24);
        if (iowild_status == 1)
        {
            lcd_show_char(120, 240, '1', LCD_RED, LCD_WHITE, 24, 0);
        }
        else{
            lcd_show_char(120, 240, '0', LCD_RED, LCD_WHITE, 24, 0);
        }
        if (iowater_status == 1)
        {
            lcd_show_char(120, 270, '1', LCD_RED, LCD_WHITE, 24, 0);
        }
        else{
            lcd_show_char(120, 270, '0', LCD_RED, LCD_WHITE, 24, 0);
        }
        LOS_Msleep(5000);
    }
        
}

/**水泵控制 0x00000001掩码打开 0x10000001掩码关闭*/
void water_conctrol_open(){
    unsigned int ret = LOS_OK;
    while (1){
        ret =LOS_EventRead(&event_water, 0x00000001, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
        iowater_status = 1;
        LOS_Msleep(1000);
    }
}
void water_conctrol_close(){
    unsigned int ret = LOS_OK;
    while (1){
        ret =LOS_EventRead(&event_water, 0x10000001, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
        iowater_status = 0;
        LOS_Msleep(1000);
    }
}
/**风扇控制 0x00000001掩码打开 0x10000001掩码关闭*/
void wild_conctrol_open(){
    unsigned int ret = LOS_OK;
    while (1){
        ret =LOS_EventRead(&event_wild, 0x00000001, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
        iowild_status = 1;
        LOS_Msleep(1000);
    }
}
void wild_conctrol_close(){
    unsigned int ret = LOS_OK;
    while (1){
        ret =LOS_EventRead(&event_wild, 0x10000001, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
        iowild_status = 0;
        LOS_Msleep(1000);
    }
}

void lcd_example()
{
    unsigned int start_id;
    unsigned int process_id;
    unsigned int wild_1_id;
    unsigned int water_1_id;
    unsigned int wild_0_id;
    unsigned int water_0_id;
    TSK_INIT_PARAM_S start = {0};
    TSK_INIT_PARAM_S process = {0};
    TSK_INIT_PARAM_S wild_1 = {0};
    TSK_INIT_PARAM_S water_1 = {0};
    TSK_INIT_PARAM_S wild_0 = {0};
    TSK_INIT_PARAM_S water_0 = {0};
    unsigned int ret = LOS_OK;
    if (ret != LOS_OK)
    {
        printf("Falied to create Message Queue ret:0x%x\n", ret);
        return;
    }
    //欢迎界面事件
    ret = LOS_EventInit(&lcd_event);
    if (ret != LOS_OK)
    {
        printf("Falied to create EventFlags\n");
        return;
    }

    /**开机界面任务 优先级0 */
    start.pfnTaskEntry = (TSK_ENTRY_FUNC)lcd_start;
    start.uwStackSize = 20480;
    start.pcName = "lcd start";
    start.usTaskPrio = 0;
    ret = LOS_TaskCreate(&start_id, &start);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
    /**数据界面任务 优先级10 */
    process.pfnTaskEntry = (TSK_ENTRY_FUNC)lcd_process;
    process.uwStackSize = 20480;
    process.pcName = "lcd process";
    process.usTaskPrio = 10;
    ret = LOS_TaskCreate(&process_id, &process);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
    //风扇控制开启任务 优先级9
    wild_1.pfnTaskEntry = (TSK_ENTRY_FUNC)wild_conctrol_open;
    wild_1.uwStackSize = 2048;
    wild_1.pcName = "wild_1";
    wild_1.usTaskPrio = 9;
    ret = LOS_TaskCreate(&wild_1_id, &wild_1);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
    //风扇控制关闭任务 优先级9
    wild_0.pfnTaskEntry = (TSK_ENTRY_FUNC)wild_conctrol_close;
    wild_0.uwStackSize = 2048;
    wild_0.pcName = "wild_0";
    wild_0.usTaskPrio = 9;
    ret = LOS_TaskCreate(&wild_0_id, &wild_0);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
    //水泵控制开启任务 优先级8
    water_1.pfnTaskEntry = (TSK_ENTRY_FUNC)water_conctrol_open;
    water_1.uwStackSize = 2048;
    water_1.pcName = "wild_1";
    water_1.usTaskPrio = 8;
    ret = LOS_TaskCreate(&water_1_id, &water_1);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
    //水泵控制关闭任务 优先级9
    water_0.pfnTaskEntry = (TSK_ENTRY_FUNC)water_conctrol_close;
    water_0.uwStackSize = 2048;
    water_0.pcName = "wild_0";
    water_0.usTaskPrio = 8;
    ret = LOS_TaskCreate(&water_0_id, &water_0);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
}


APP_FEATURE_INIT(lcd_example);
